const { contextBridge, ipcRenderer, desktopCapturer, remote} = require('electron')
const {Menu, MenuItem} = remote;
const  signalTypes = require('../../../signal_types')
const {initPeerPuppet} = require('./peer-puppet');
const {log} = require('../../../utils/log');

log(process.version)

contextBridge.exposeInMainWorld(
  'electron',
  {
    closeMainWin(){
      ipcRenderer.send(signalTypes.CLOSE_MAIN_WINDOW);
    },
    minimizeMainWin(){
      ipcRenderer.send(signalTypes.MINIMIZE_MAIN_WINDOW);
    },
    onSignalStatusChange(cb){
      log('--- --- ---onSignalStatusChange--- --- ---');
      ipcRenderer.on(signalTypes["SIGNAL_SERVER:STATE_CHANGED"], (e,status) => {
        log('signalStatusChange--- --- ---');
        cb(status);
      });
    },
    tryToConnectForSeconds(){
      ipcRenderer.send(signalTypes["SIGNAL_SERVER:TRY_TO_CONNECT_FOR_SECONDS"]);
    },
    onLogined(cb){
      log('--- --- ---onLogined--- --- ---');
      ipcRenderer.on(signalTypes["SIGNAL_SERVER:LOGINED"], (e,identifier_code) => {
        log('received identifier_code--- --- ---:',identifier_code);
        cb(identifier_code);
      });
    },
    controlRemote(data){
      ipcRenderer.send(signalTypes["SIGNAL_SERVER:CONTROL"], data);
    },


    onToBeControlled(){
      log('--- --- --- onToBeControlled --- --- ---');
      ipcRenderer.on(signalTypes["SIGNAL_SERVER:BE_CONTROLLED"], async (e,controller_code,)=>{
        log('--- --- --- initPeerPuppet --- --- ---');
        await initPeerPuppet(controller_code);
        //todo 是否支持被多个人控制？ 支持的话 交换信息时 也要带上对方的code用以区分哪个市哪个
      });
    },
    onContextMenu () {
      const menu = new Menu();
      menu.append(new MenuItem({label: '全选', role: 'selectAll'}));
      //todo 怎么确保有选中的内容 才让↓复制菜单按钮激活
      menu.append(new MenuItem({label: '复制', role: 'copy'}));
      menu.popup()
    }
  }
)
